import os
import re
import threading
import urllib
import urllib2

class Timer(threading.Thread):

    """docstring for ClassName"""
    def __init__(self, url="http://www.500.com", dirname="images"):
        super(Timer, self).__init__()
        self.url = url
        self.dirname = dirname

    def run(self):
        res = urllib2.urlopen(self.url).read()
        imgs = re.findall(r"([^\'\"\(]+\/([^\/]+)\.(jpg|gif|png))", res)
        for i in imgs:
            path = os.path.join(".", self.dirname).replace("\\", "/")
            if not os.path.isdir(path):
                os.mkdir(path)

            path = os.path.join(path, self.url.rsplit("/")[-1]).replace("\\", "/")
            if not os.path.isdir(path):
                os.mkdir(path)

            filepath = os.path.join(path, ".".join(i[1:])).replace("\\", "/")
            res = urllib.urlretrieve(i[0], filepath)
            print res

def main():
    ulist = ["http://www.baidu.com", "http://www.500.com"]
    ths = []

    for u in ulist:
        th = Timer(u)
        th.start()
        ths.append(th)

    for th in ths:
        th.join()

if __name__ == "__main__":
    main()